home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWRefCnt / FWCouPtr.h < prev    next >
Encoding:
Text File  |  1996-09-17  |  5.9 KB  |  203 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWCouPtr.h
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWCOUPTR_H
  11. #define FWCOUPTR_H
  12.  
  13. #ifndef FWEXCLIB_H
  14. #include "FWExcLib.h"
  15. #endif
  16.  
  17. //========================================================================================
  18. // CLASS FW_TCountedPtr<tRep>
  19. //========================================================================================
  20.  
  21. template <class tRep>
  22. class FW_TCountedPtr
  23. {
  24. public:
  25.     FW_DECLARE_AUTO(FW_TCountedPtr<tRep>)
  26.  
  27.     ~FW_TCountedPtr();
  28.         // Destroy the pointer.  
  29.         // Decrements the count in the rep, and deletes the rep if count is zero.
  30.         
  31.     FW_TCountedPtr();
  32.         // Creates a "NULL" pointer.
  33.  
  34.     FW_TCountedPtr(const FW_TCountedPtr<tRep>& other);
  35.         // Point this pointer to the same rep as the other pointer points to.
  36.  
  37.     FW_TCountedPtr(tRep* rep);
  38.         // Creates a pointer pointing at rep.
  39.     
  40.     FW_TCountedPtr<tRep>& operator=(const FW_TCountedPtr<tRep>& other);
  41.         // Assignment, point this ponter to the same rep as the other pointer points to.
  42.         
  43.     FW_TCountedPtr<tRep>& operator=(tRep* rep);
  44.         // Assignment, point this ponter to rep.
  45.         
  46.     tRep* operator->() const { return fRep; }
  47.         // Provide access to members of rep.    
  48.         
  49.     tRep& operator*() const { return *fRep; }
  50.         // Provide access to rep.
  51.         // This operator should be used only if operator->() is not sufficient.
  52.     
  53.     operator const void*() const;
  54.         // Use to test if this is a "NULL" pointer.
  55.  
  56. protected:
  57.     void    SetRep(tRep* rep);
  58.     
  59. protected:    
  60.     tRep *fRep;
  61. };
  62.  
  63. //----------------------------------------------------------------------------------------
  64. // FW_TCountedPtr<tRep>::FW_TCountedPtr
  65. //----------------------------------------------------------------------------------------
  66.  
  67. template <class tRep>
  68. inline FW_TCountedPtr<tRep>::FW_TCountedPtr() :
  69.     fRep(NULL)
  70. {
  71.     FW_END_CONSTRUCTOR
  72. }
  73.  
  74. //----------------------------------------------------------------------------------------
  75. // FW_TCountedPtr<tRep>::~FW_TCountedPtr
  76. //----------------------------------------------------------------------------------------
  77.  
  78. template <class tRep>
  79. inline FW_TCountedPtr<tRep>::~FW_TCountedPtr()
  80. {
  81.     FW_START_DESTRUCTOR
  82.     if (fRep && !fRep->DecrementReferenceCount()) delete fRep;
  83. }
  84.  
  85. //----------------------------------------------------------------------------------------
  86. // FW_TCountedPtr<tRep>::FW_TCountedPtr
  87. //----------------------------------------------------------------------------------------
  88.  
  89. template <class tRep>
  90. inline FW_TCountedPtr<tRep>::FW_TCountedPtr(const FW_TCountedPtr& other) :
  91.     fRep(other.fRep)
  92. {
  93.     if (fRep) fRep->IncrementReferenceCount();
  94.     FW_END_CONSTRUCTOR
  95. }
  96.  
  97. //----------------------------------------------------------------------------------------
  98. // FW_TCountedPtr<tRep>::FW_TCountedPtr
  99. //----------------------------------------------------------------------------------------
  100.  
  101. template <class tRep>
  102. inline FW_TCountedPtr<tRep>::FW_TCountedPtr(tRep* rep) :
  103.     fRep(rep)
  104. {
  105.     if (fRep) fRep->IncrementReferenceCount();
  106.     FW_END_CONSTRUCTOR
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. // FW_TCountedPtr<tRep>::SetRep
  111. //----------------------------------------------------------------------------------------
  112.  
  113. template <class tRep>
  114. inline void FW_TCountedPtr<tRep>::SetRep(tRep* rep)
  115. {
  116.     if (fRep != rep)
  117.     {
  118.         if (fRep && !fRep->DecrementReferenceCount()) delete fRep;
  119.         fRep = rep;
  120.         if (fRep) fRep->IncrementReferenceCount();
  121.     }
  122. }
  123.         
  124. //----------------------------------------------------------------------------------------
  125. // FW_TCountedPtr<tRep>::operator=
  126. //----------------------------------------------------------------------------------------
  127.  
  128. template <class tRep>
  129. inline FW_TCountedPtr<tRep>& FW_TCountedPtr<tRep>::operator=(const FW_TCountedPtr<tRep>& other)
  130. {
  131.     SetRep(other.fRep);
  132.     return *this;
  133. }
  134.  
  135. //----------------------------------------------------------------------------------------
  136. // FW_TCountedPtr<tRep>::operator=
  137. //----------------------------------------------------------------------------------------
  138.  
  139. template <class tRep>
  140. inline FW_TCountedPtr<tRep>& FW_TCountedPtr<tRep>::operator=(tRep* rep)
  141. {
  142.     SetRep(rep);
  143.     return *this;
  144. }
  145.  
  146. //----------------------------------------------------------------------------------------
  147. // FW_TCountedPtr<tRep>::operator=
  148. //----------------------------------------------------------------------------------------
  149.  
  150. template <class tRep>
  151. inline FW_TCountedPtr<tRep>::operator const void*() const
  152. {
  153.     return fRep;
  154. }
  155.  
  156. //========================================================================================
  157. // CLASS FW_MCountedPtrRep
  158. //========================================================================================
  159.  
  160. class FW_MCountedPtrRep
  161. {
  162. public:
  163.         
  164.     FW_MCountedPtrRep();
  165.     virtual ~FW_MCountedPtrRep();
  166.  
  167.     long    IncrementReferenceCount();
  168.     long    DecrementReferenceCount();
  169.     long    GetReferenceCount();
  170.     
  171. private:
  172.     long     fRefCount;
  173. };
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // FW_MCountedPtrRep::DecrementReferenceCount
  177. //----------------------------------------------------------------------------------------
  178.  
  179. inline long FW_MCountedPtrRep::DecrementReferenceCount()
  180. {
  181.     return --fRefCount;
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. // FW_MCountedPtrRep::IncrementReferenceCount
  186. //----------------------------------------------------------------------------------------
  187.  
  188. inline long FW_MCountedPtrRep::IncrementReferenceCount()
  189. {
  190.     return ++fRefCount;
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // FW_MCountedPtrRep::GetReferenceCount
  195. //----------------------------------------------------------------------------------------
  196.  
  197. inline long FW_MCountedPtrRep::GetReferenceCount()
  198. {
  199.     return fRefCount;
  200. }
  201.  
  202. #endif
  203.